$(function() {
    //页面初始化
   init();
    function init(){
              var loading = '<dl><div class="loading">加载中...</div></dl>'; 
              $(".theSamewap").append(loading);
              //获取咨询
              $.get(jumpUrl+'/question/'+id+'/similar',function(data){

                }, "json").error(function(data) {
                  
                }).success(function(data) {
                  $(".loading").parent().remove();
                  var str ="";
                  var obj = data.data;
                  // console.info(obj)
                  
                  //显示回复内容
                  for(var i=0; i<obj.length; i++)  
                  {
                    obj[i].view_count == undefined ? obj[i].view_count =0 : obj[i].view_count;
                    obj[i].likes_count == undefined ? obj[i].likes_count =0 : obj[i].likes_count;

                    if(i<2){
                      str +='<div class="theSameType">'
                      str +='<dl class="theIssueClass clear">'
                            str +='<dt><b>问题分类</b></dt>'
                            str +='<dd><b>'+obj[i].title+'</b></dd>'
                        str +='</dl>'
                        str +='<div class="theContxt">'+obj[i].content+'</div>'
                        str +='<dl class="issueClassInfo clear">'
                            str +='<dd>'
                                str +='<label class="dateTime">'+obj[i].created_at+'</label>'
                                str +='<div class="numlr" data-id="'+obj[i].id+'">'
                                      str +='<a href="javascript:void(0)" title="阅读"><label class="readNumber">'+obj[i].view_count+'</label></a><b class="line">|</b>'
                                      str +='<a class="likes_count" href="javascript:void(0)" title="点赞"><label style="padding-right: 0px;" class="like">'+obj[i].likes_count+'</label></a>'
                                  str +='</div>'
                            str +='</dd>'
                        str +='</dl>'
                      str +='</div>';
                      continue;
                    }
                      break;     
                  }

                  $(".theSamewap").append(str);
                  layer.close(loading);

                });
            }
          //点赞
         $(document).on('click', '.likes_count', function(e) {
            var $this = $(this);
            var item_id = $this.parent().attr("data-id");
            var token = $("input:[name='_token']").val();
            var params = {};
            params['item_type'] = 'post';
            params['item_id'] = item_id;
            params['_token'] = token;
            // console.info(params)
            var url = jumpUrl+'/like';
            $.post(url,params,function(data){

                }, "json").error(function(data) {
                  var error =  $.parseJSON(data.responseText);
                  layer.msg(error.error_messages.item_type[0]);
                }).success(function(data) {
                  $this.children('.like').text(data['likes_count']);
                })

            stopDefault(e);
          })


         
    
    var comment_form = $("#comment_form");
    //用户评论
    var stch=false; //防止多次提交的全局变量
    $(document).on("click",".subComment",function(e){
        var comment =$.trim($(".js_comment_txtarea").val());
        var str="";
        if(comment.length < 0){
          layer.msg('提交的内容不能为空！');
        }else{
          if (stch==true)
          {
            layer.msg('请不要重新提交数据!');
          }else{
            stch=true;
            $.post(comment_form[0].action, comment_form.serialize(),
              function(data){
                
              },'json').error(function(data) {
                  var obj = $.parseJSON(data.responseText);
                  $.each(obj.error_messages, function(i, item){
                    // console.info(item[0]);
                    layer.msg(item[0]);
                  });
              }).success(function(data) {
                  var obj = data.data;
                    str+='<ol class="reply clear">'
                    if(obj.from_creator == true){
                          str+='<li class="replyPortrait">'
                            str+='<img class="" src="'+obj.creator_avatar+'" alt="头像"><br>'
                            str+='<label>'+obj.creator_name+'</label>'
                          str+='</li>'
                          str+='<li class="replyDetail">'
                            str+='<div class="replyTime">'+obj.created_at+'</div>'
                            str+='<div class="replayContents">'+obj.content+'</div>'
                          str+='</li>'
                    }else{
                          str+='<li class="replyDetail">'
                            str+='<div class="replyTime">'+obj.created_at+'</div>'
                            str+='<div class="replayContents">'+obj.content+'</div>'
                          str+='</li>'
                          str+='<li class="replyPortrait">'
                            str+='<img class="" src="'+obj.creator_avatar+'" alt="头像"><br>'
                            str+='<label>'+obj.creator_name+'</label>'
                          str+='</li>'
                    }
                    str+='</ol>';
                  $(".lawyerReply").append(str);
                  $(':input','#comment_form').not('textarea :submit, :reset, :hidden').val(''); 
                  layer.msg("评论成功！");
                  stch=false;
                  //清空form的数据
              });
          }
        }
        stopDefault(e);
    });


    //在输入评论的时候把值付给 文本
    $(document).on( "keyup", "form textarea", function(e) {
        var $this = $(this);
        var val = $.trim($this.val());
        $this.siblings(".ds-hidden-text").text(val)
        stopDefault(e);
    })


 getcontimg();
var getcontimg = "";
function getcontimg(){
    var loading = '<dl><div class="loading">加载中...</div></dl>'; 
    $(".contQues-img").append(loading);
    $.get(jumpUrl+"/lawyer/list_lawyers?rows_per_page=12",function(data){

    }, "json").error(function(data) {
        layer.msg("请求失败！")
    }).success(function(data) {
      $(".loading").parent().remove();
      var obj = data.data;
      getcontimg = obj;
      if(obj.length > 1){
        
        for(var i=0;i<obj.length;i++){
          var str ='<li><a href="'+jumpUrl+'/lawyer/'+obj[i].id+'" rel="nofollow"><img src="'+obj[i].avatar+'" alt=""></a></li>';
          $(".contQues-img").append(str);
          if(i == 0){
                $(".lawyerPortrait").attr("src",obj[i].avatar);
                $(".thisInfo .name").text(obj[i].realname);
                $(".thisInfo .practitioners").text(obj[i].practice_period);

                var work_scope = obj[i].work_scope;
                // console.info(work_scope)
                var options = '';
                $.each(work_scope, function(key, item) {
                    options+='<label value="'+item.key+'">'+item.value+'</label> &nbsp;&nbsp;';
                });
                $(".thisInfo .pracScope").append(options);

                var introduce = obj[i].introduce;
                if(introduce != null){
                  if(introduce.length > 120){
                    introduce = introduce.substr(0,120)+'......';
                  }else{
                    introduce = introduce;
                  }
                }else{
                  introduce = "";
                }
                $(".personalProfile").text(introduce);
          }
        }
      }else{
         var str ='<li>暂无数据</li>';
        $(".contQuesContxt").hide();
        $(".contQues-img").append(str);
      }

    });
}
  //不想等待直接联系律师  /*is_recommended=1*/
  $(document).on( "mouseover", ".contQues-img li", function(e) { 
      var list = getcontimg;
      var index = $(this).index();
      $(".lawyerPortrait").attr("src","");
      $(".thisInfo .name").text("");
      $(".thisInfo .practitioners").text("");
      $(".thisInfo .pracScope").text("");
      $(".personalProfile").text("");
        for(var i=0;i<list.length;i++){
          if(i == index){
                $(".lawyerPortrait").attr("src",list[i].avatar);
                $(".thisInfo .name").text(list[i].realname);
                $(".thisInfo .practitioners").text(list[i].practice_period);
                var work_scope = list[i].work_scope;
                var options = '';
                $.each(work_scope, function(key, item) {
                    options+='<label value="'+item.key+'">'+item.value+'</label> &nbsp;&nbsp;';
                });
                $(".thisInfo .pracScope").append(options);
                var introduce = list[i].introduce;
                if(introduce != null){
                  if(introduce.length > 120){
                    introduce = introduce.substr(0,120)+'......';
                  }else{
                    introduce = introduce;
                  }
                }else{
                  introduce = "";
                }
                $(".personalProfile").text(introduce);
          }
        }
      stopDefault(e);
  });
  
  //对咨询回复的评分
  $(document).on("mouseover", ".star-score img", function(){

  	var index = $(this).index();
  	var	starOnUrl = jumpUrl+"/img/star-on.png";
	var	starOffUrl = jumpUrl+"/img/star-off.png";
  	$(this).attr("src",starOnUrl);
  	$(this).prevAll().attr("src",starOnUrl);
  	$(this).nextAll().attr("src",starOffUrl);

  });
  
  $(document).on("click", ".star-score img", function(){
		$('.num-score').text($(this).index()+1);
		$('#evaluate_score').val($(this).index()+1);		
  })
  
  $(document).on("mouseleave", ".star-score", function(){
	  var index = parseInt($('.num-score').text());
	  var starOffUrl = jumpUrl+"/img/star-off.png";
	  var starOnUrl = jumpUrl+"/img/star-on.png";
	  $('.star-score img:eq('+index+')').prevAll().attr("src",starOnUrl);
	  $('.star-score img:eq('+index+')').attr("src",starOffUrl);
	  $('.star-score img:eq('+index+')').nextAll().attr("src",starOffUrl);	  

	  });
  
  
  
  var to_complete = $("#to_complete");
  //用户评论
  var stch=false; //防止多次提交的全局变量
  $(document).on("click",".subComplete",function(e){
	  var starOnUrl = jumpUrl+"/img/star-on.png";
	  var starOffUrl = jumpUrl+"/img/star-off.png";
	  var score = $('#evaluate_score').val();
      var comment =$.trim($(".js_comment_txtarea").val());
      var str="";
      if(comment.length < 0){
        layer.msg('提交的内容不能为空！');
      }else if(score<1){
    	layer.msg('评分在1-5分之间'); 
      }else{
      
        if (stch==true)
        {
          layer.msg('请不要重新提交数据!');
        }else{
          stch=true;
          $.post(to_complete[0].action, to_complete.serialize(),
            function(data){
              
            },'json').error(function(data) {
                var obj = $.parseJSON(data.responseText);
                $.each(obj.error_messages, function(i, item){
                  // console.info(item[0]);
                  layer.msg(item[0]);
                });
            }).success(function(data) {
            	var obj = data.data;
            	var str = "";
            	str+='<div class="ds-comments-info">'
            		str+='<span class="title">评价分数:</span>'
            		str+='<span class="star-score-done">'
            	for(var i=1; i<=5; i++){
            		if(i<=obj.score){
            			str+='<img src="'+starOnUrl+'">'
            		}else{
            			str+='<img src="'+starOffUrl+'">'
            		}
            	}
            		str+='</span>'
            		str+='<span class="num-score-done"> '+obj.score+' 分</span>'
            		str+='<div class="discussContext">评价内容：'
            		str+=obj.comment;
            		str+='</div>'
            	str+="</div>"	
            		
 
                layer.msg("评价成功");
            	$(".discuss").html(str);                
                stch=false;
                //清空form的数据
            });
        }
      }
      stopDefault(e);
  });
  
  //收藏文章
    $(document).on("click", ".favorite", function(e){
      var url = jumpUrl+"/collect";
      var _token = $("input[name='_token']").val();
      $.post(url,{'collect_type':'1', 'item_id':id, '_token':_token},function(){
      }, 'json').error(function(data){
            var obj = $.parseJSON(data.responseText);
            $.each(obj.error_messages, function(i, item){
              layer.msg(item[0]);
            });
      }).success(function(data){
        var obj = data.data;
          layer.msg('收藏成功');
          $('.favorite-off').addClass('favorite-on').removeClass('favorite-off')
          $('.num').text(obj)
          $('label.last').text(obj);
      });
    });          
});

//最活跃的律师
$(document).ready(function(){
    var active_url = jumpUrl+"/lawyer/list_lawyers?order_by=active&rows_per_page=5";
    var loading = '<li><div class="loading">加载中...</div><br/></li>'; 
    $(".rightlv ul").append(loading);
    $.get(active_url, function(data){        
        }, "json").error(function(data) {
          var status_code = $.parseJSON(data.responseText);
          layer.msg(status_code.message);
        }).success(function(data) { 
            var obj = data.data;
            // console.info(obj)
            var work_scope = obj;
             for(var i=0; i<obj.length;i++){
                var options = '';
                $.each(work_scope, function(key, item) {
                  options+='<li>'
                    options+='<img class="lvimg" src="'+item.avatar+'">'
                    options+='<div class="lvtextbox">'
                      options+='<p><a href="'+jumpUrl+'/lawyer/'+item.id+'" class="lvname">'+item.realname+'</a>&nbsp;<span style="color:#004799">律师</span></p>'
                      options+='<p><span>'+item.practice_period+'</span> <span>'+item.province+'</span></p>'
                      options+='<p>'
                          for (var i=0; i<item.average_evaluate_score; i++) {
                          options+='<img src="'+jumpUrl+'/img/star-on.png">'
                          }
                          for (var i=0; i<5-item.average_evaluate_score; i++) {
                          options+='<img src="'+jumpUrl+'/img/star-off.png">'
                          }
                       options+='</p>'
                      options+='<p>评价&nbsp;'+item.average_evaluate_score+'&nbsp;案例&nbsp;'+item.free_reply_count+'</p>'
                    options+='</div>'
                  options+='</li>';
                });
              };
                $(".rightlv ul").append(options);
        });
});



